// pages/user_proxy/money.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		data: [],
		page: 1,
		more: true,

		tabKey: 'loadData',
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.tryAllowShareMessage();
		this.onPullDownRefresh();
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
		wx.$getServerUserInfo(true, {
			is_refresh_balance: 1
		}).then((res) => {
			this.setData({
				user: res
			})
		});
		this[this.data.tabKey](1).then(function () {
			wx.stopPullDownRefresh();
		});
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		if (!this.more) {
			return;
		}

		this[this.data.tabKey](this.data.page + 1);
	},

	/**
	 * 切换Tab
	 */
	tabChange: function (e) {
		const key = e.currentTarget.dataset.key;
		this.data.tabKey = key;
		this.onPullDownRefresh();
		this.setData({
			data: [],
			tabKey: key
		});
	},

	/**
	 * 加载收益明细
	 */
	loadData: function (page) {
		this.data.page = page;
		return wx.$model.user.balanceLogs({
			page: page,
		}).then((res) => {
			const {
				data,
				per_page
			} = res;
			const oldData = this.data.data;
			this.setData({
				data: page === 1 ? data : oldData.concat(data),
				more: data.length < per_page
			});
		}).resolved();
	},

	/**
	 * 加载我的提现记录
	 */
	loadCashData: function (page) {
		this.data.page = page;
		return wx.$model.store.cashList({
			page: page,
		}).then((res) => {
			const {
				data,
				per_page
			} = res;
			const oldData = this.data.data;
			this.setData({
				data: page === 1 ? data : oldData.concat(data),
				more: data.length < per_page
			});
		}).resolved();
	},

	/**
	 * 提现申请
	 */
	gotoCash: function () {
		wx.$getServerUserInfo().then((res) => {
			if (!res.store.alipay_account || !res.store.bank_name || !res.store.bank_type) {
				return wx.navigateTo({
					url: '/pages/user_proxy/money_id'
				});
			}
			return wx.$model.store.cashApply({}, {
				loading: true
			}).then((res) => {
				wx.showModal({
					content: '已提交申请！',
					showCancel: false,
					success: () => {
						wx.startPullDownRefresh();
					}
				});
			});
		})
	}
})